package Hot100;

import LinkedList.ListNode;

public class 两数相加 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode l3 = new ListNode();
        int carry = 0;
        ListNode l = l3;
        while (l1 != null || l2 != null) {
            int x = l1 != null ? l1.val : 0;
            int y = l2 != null ? l2.val : 0;
            int z = x + y + carry;
            carry = z / 10;
            z = z % 10;
            l3.next = new ListNode(z);
            l3 = l3.next;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        if (carry == 1) l3.next = new ListNode(carry);
        return l.next;
    }
}
